import { PrismaClient, PlayerRank, PlayerPosition } from '@prisma/client'

const prisma = new PrismaClient()

async function main() {
  console.log('🌱 开始种子数据填充...')

  // 清除现有数据
  await prisma.playerPositionMapping.deleteMany()
  await prisma.player.deleteMany()
  console.log('🗑️  清除现有数据')

  // 20个选手数据
  const players = [
    {
      id: '10001',
      nickname: 'Miracle-',
      rank: PlayerRank.IMMORTAL,
      highestMmr: 12000,
      positions: [PlayerPosition.CARRY, PlayerPosition.MID]
    },
    {
      id: '10002', 
      nickname: 'w33',
      rank: PlayerRank.DIVINE,
      highestMmr: 8500,
      positions: [PlayerPosition.MID]
    },
    {
      id: '10003',
      nickname: 'MinD_ContRoL',
      rank: PlayerRank.DIVINE,
      highestMmr: 8200,
      positions: [PlayerPosition.OFFLANE]
    },
    {
      id: '10004',
      nickname: 'GH',
      rank: PlayerRank.IMMORTAL,
      highestMmr: 9800,
      positions: [PlayerPosition.SUPPORT, PlayerPosition.HARD_SUPPORT]
    },
    {
      id: '10005',
      nickname: 'KuroKy',
      rank: PlayerRank.ANCIENT,
      highestMmr: 7500,
      positions: [PlayerPosition.HARD_SUPPORT]
    },
    {
      id: '10006',
      nickname: 'Arteezy',
      rank: PlayerRank.IMMORTAL,
      highestMmr: 11500,
      positions: [PlayerPosition.CARRY]
    },
    {
      id: '10007',
      nickname: 'SumaiL',
      rank: PlayerRank.DIVINE,
      highestMmr: 8800,
      positions: [PlayerPosition.MID, PlayerPosition.CARRY]
    },
    {
      id: '10008',
      nickname: 'Universe',
      rank: PlayerRank.LEGEND,
      highestMmr: 6200,
      positions: [PlayerPosition.OFFLANE]
    },
    {
      id: '10009',
      nickname: 'Puppey',
      rank: PlayerRank.ANCIENT,
      highestMmr: 7800,
      positions: [PlayerPosition.HARD_SUPPORT]
    },
    {
      id: '10010',
      nickname: 'Yatoro',
      rank: PlayerRank.IMMORTAL,
      highestMmr: 11800,
      positions: [PlayerPosition.CARRY]
    },
    {
      id: '10011',
      nickname: 'Collapse',
      rank: PlayerRank.DIVINE,
      highestMmr: 9200,
      positions: [PlayerPosition.OFFLANE]
    },
    {
      id: '10012',
      nickname: 'Mira',
      rank: PlayerRank.IMMORTAL,
      highestMmr: 9500,
      positions: [PlayerPosition.SUPPORT]
    },
    {
      id: '10013',
      nickname: 'TORONTOTOKYO',
      rank: PlayerRank.DIVINE,
      highestMmr: 8900,
      positions: [PlayerPosition.MID]
    },
    {
      id: '10014',
      nickname: 'Miposhka',
      rank: PlayerRank.ANCIENT,
      highestMmr: 7600,
      positions: [PlayerPosition.HARD_SUPPORT]
    },
    {
      id: '10015',
      nickname: 'Ame',
      rank: PlayerRank.IMMORTAL,
      highestMmr: 11600,
      positions: [PlayerPosition.CARRY]
    },
    {
      id: '10016',
      nickname: 'Maybe',
      rank: PlayerRank.DIVINE,
      highestMmr: 9100,
      positions: [PlayerPosition.MID]
    },
    {
      id: '10017',
      nickname: 'Faith_bian',
      rank: PlayerRank.DIVINE,
      highestMmr: 8400,
      positions: [PlayerPosition.OFFLANE]
    },
    {
      id: '10018',
      nickname: 'y\'',
      rank: PlayerRank.IMMORTAL,
      highestMmr: 9700,
      positions: [PlayerPosition.SUPPORT]
    },
    {
      id: '10019',
      nickname: 'xNova',
      rank: PlayerRank.ANCIENT,
      highestMmr: 7300,
      positions: [PlayerPosition.HARD_SUPPORT]
    },
    {
      id: '10020',
      nickname: 'Topson',
      rank: PlayerRank.IMMORTAL,
      highestMmr: 11000,
      positions: [PlayerPosition.MID]
    }
  ]

  // 创建选手和位置数据
  for (const playerData of players) {
    const { positions, ...playerInfo } = playerData
    
    // 创建选手
    const player = await prisma.player.create({
      data: playerInfo
    })
    
    // 创建选手位置映射（无外键约束）
    for (const position of positions) {
      await prisma.playerPositionMapping.create({
        data: {
          playerId: player.id,
          position: position
        }
      })
    }
    
    console.log(`✅ 创建选手: ${player.nickname} (${positions.join(', ')})`)
  }

  console.log(`🎉 成功创建 ${players.length} 个选手数据`)
}

main()
  .catch((e) => {
    console.error('❌ 种子数据填充失败:', e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  })
